package com.kzj.domain.bk;

import com.kzj.common.constant.GlobalConstant;

import java.util.Random;

/**
 * 算式
 */
public class EquationBP3 {
    /**
     * 符号
     */
    private String symbol;
    /**
     * 计算数1
     */
    private Integer num1;
    /**
     * 计算数2
     */
    private Integer num2;
    public EquationBP3(){}
    public EquationBP3(Integer num1, String symbol, Integer num2)
    {
        this.num1=num1;
        this.symbol=symbol;
        this.num2=num2;
    }

    /**
     * 计算算式结果
     * @param equationBP3 算式
     * @return 算式的计算答案
     */
    public static Integer calculateResult(EquationBP3 equationBP3)
    {
        if(equationBP3.getSymbol().equals(GlobalConstant.SYMBOL_ADD))
        {
            return equationBP3.getNum1() + equationBP3.getNum2();
        }
        else if(equationBP3.getSymbol().equals(GlobalConstant.SYMBOL_SUB))
        {
            return equationBP3.getNum1() - equationBP3.getNum2();
        }
        else if(equationBP3.getSymbol().equals(GlobalConstant.SYMBOL_MUL))
        {
            return equationBP3.getNum1() * equationBP3.getNum2();
        }
        else if(equationBP3.getSymbol().equals(GlobalConstant.SYMBOL_DIV))
        {
            return equationBP3.getNum1() / equationBP3.getNum2();
        }
        else return -1;
    }

    /**
     * 将算式和算式的结果一起以string的方式返回
     * @param equationBP3 算式
     * @return 算式和算式的结果一起以string的方式
     */
    public static String calculateResultToString(EquationBP3 equationBP3)
    {
        return equationBP3.toString()+ EquationBP3.calculateResult(equationBP3);
    }

    /**
     * 随机生成0-limit的加法
     * @param limit 计算数的范围
     * @return 算式
     */
    public static EquationBP3 generateAdditionEquation(Integer limit)
    {
        Random random = new Random();
        Integer num1= random.nextInt(limit+1);
        Integer num2=random.nextInt(limit+1-num1);
        return new EquationBP3(num1,GlobalConstant.SYMBOL_ADD,num2);
    }

    /**
     * 随机生成0-limit的减法
     * @param limit 计算数的范围
     * @return 算式
     */
    public static EquationBP3 generateSubstractEquation(Integer limit)
    {
        Random random = new Random();
        Integer num1= random.nextInt(limit+1);
        Integer num2=random.nextInt(num1+1);
        return new EquationBP3(num1,GlobalConstant.SYMBOL_SUB,num2);
    }
    /**
     * 随机生成0-limit的乘法
     * @param limit 计算数的范围
     * @return 算式
     */
    public static EquationBP3 generateMulEquation(Integer limit)
    {
        Random random = new Random();
        Integer num1= random.nextInt(limit+1);
        Integer num2= random.nextInt(limit+1);
        return new EquationBP3(num1,GlobalConstant.SYMBOL_MUL,num2);
    }
    /**
     * 随机生成0-limit的除法
     * @param limit 计算数的范围
     * @return 算式
     */
    public static EquationBP3 generateDivEquation(Integer limit)
    {
        Random random = new Random();
        Integer num1= random.nextInt(limit)+1;
        Integer num2= random.nextInt(num1+1)*num1;
        return new EquationBP3(num2,GlobalConstant.SYMBOL_DIV,num1);
    }

    /**
     * 随机生成0-limit以内的加减法
     * @param limit 计算数的范围
     * @return 算式
     */
    public static EquationBP3 generateEquation(Integer limit)
    {
        Random random=new Random();
        if(random.nextInt(2)==0)
        {
            return EquationBP3.generateAdditionEquation(limit);
        }
        else return EquationBP3.generateSubstractEquation(limit);
    }

    /**
     * 判断与目标Equation是否相同
     * @param obj 算式
     * @return ture:算式完全相同 false:算式不同
     */
    public boolean equals(EquationBP3 obj) {
        if(obj==null)
            return false;
        return this.num1.equals(obj.getNum1())&this.num2.equals(obj.getNum2())&this.symbol.equals(obj.getSymbol());

    }

    public String getSymbol() {
        return symbol;
    }

    public void setSymbol(String symbol) {
        this.symbol = symbol;
    }

    public void setNum1(Integer num1) {
        this.num1 = num1;
    }

    public void setNum2(Integer num2) {
        this.num2 = num2;
    }

    public Integer getNum1() {
        return num1;
    }

    public Integer getNum2() {
        return num2;
    }

    @Override
    public String toString() {
        return this.num1+" "+this.symbol+" "+this.num2+" = ";
    }
}
